package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.User;
import service.Factory.ServiceFactory;

/**
 * Servlet implementation class DeleteCollectionServlet
 */
@WebServlet("/DeleteCollectionServlet")
public class DeleteCollectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteCollectionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User curUser = (User)session.getAttribute("_user");
		String collection_id = request.getParameter("cid");
		String product_id = request.getParameter("pid");
		
		if (curUser == null || collection_id == null || product_id == null) {
			response.sendRedirect(request.getContextPath()+ "/HomeServlet");
			return;
		}
		
		try {
			ServiceFactory.getCollectionServiceInstance().deleteCollection(Integer.parseInt(collection_id));
			response.sendRedirect(request.getContextPath()+ "/ProductDetailServlet?id=" + product_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			response.sendRedirect(request.getContextPath()+ "/HomeServlet");
		}
	}

}
